﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BG_Manage_BG_News : System.Web.UI.Page
{
    VPINews.BLL.News bll_news = new VPINews.BLL.News();
    VPINews.Model.News mod_news = new VPINews.Model.News();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            getType();
            getNews();
        }

    }

    private void getType()									 //显示TypeName控件的下拉列表	
    {
        VPINews.BLL.NewsType bll_type = new VPINews.BLL.NewsType();

        DataSet ds = bll_type.GetAllList();
        int rows = ds.Tables[0].Rows.Count;
        while (rows > 0)
        {
            string tName = ds.Tables[0].Rows[rows - 1]["TypeName"].ToString();

            listTypeName.Items.Add(new ListItem(tName));
            rows--;
        }

        this.listTypeName.DataBind();
    }


    private void getNews()
    {
        GridView1.AllowPaging = true;//是否启用分页
        GridView1.PageSize = 8;//每页显示条数

        this.GridView1.DataSource = bll_news.GetAllList();
        this.GridView1.DataBind();

        //如果当前索引为0 则 禁用 首页 上一页 按钮
        if (GridView1.PageIndex == 0)
        {
            this.LinkButton1.Enabled = false;
            this.LinkButton2.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton1.Enabled = true;
            this.LinkButton2.Enabled = true;
        }
        //如果当前索引为-1 则 禁用 下一页 尾页 按钮 按钮
        if (GridView1.PageIndex == GridView1.PageCount - 1)
        {
            this.LinkButton3.Enabled = false;
            this.LinkButton4.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton3.Enabled = true;
            this.LinkButton4.Enabled = true;
        }


    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.Visible)
        {
            if (this.listChoice.SelectedItem.Text == "标题")
            {
                mod_news.Title = this.txtSearch.Text;
                string sqlwhere = "Title='" + this.txtSearch.Text + "'";
                this.GridView1.DataSource = bll_news.GetList(sqlwhere);
                this.GridView1.DataBind();
            }
            else
            {
                mod_news.Contents = this.txtSearch.Text;
                string sqlwhere = "Contents='" + this.txtSearch.Text + "'";
                this.GridView1.DataSource = bll_news.GetList(sqlwhere);
                this.GridView1.DataBind();
            }
        }


    }


    protected void cheAll_CheckedChanged(object sender, EventArgs e)
    {
        //循环每一行
        foreach (GridViewRow g in GridView1.Rows)
        {
            //在模板中寻找CheckBox2空间ID
            CheckBox c = (CheckBox)g.Cells[0].FindControl("cheOne");
            if (!c.Checked)
            {
                c.Checked = true;
            }
            else
            {
                c.Checked = false;
            }
        }
    }

    //当CheckBox选中后,批量删除
    protected void btnDelche_Click1(object sender, EventArgs e)
    {
        //循环每一行
        foreach (GridViewRow g in GridView1.Rows)
        {
            //在模板中寻找CheckBox: cheOne 空间ID
            CheckBox c = (CheckBox)g.Cells[0].FindControl("cheOne");
            if (!c.Checked)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", "<Script>alert('没有被选中项')</Script>");
            }
            else
            {
                int id = Convert.ToInt32(g.FindControl("NewsID").ToString());
                VPINews.Model.News mod_news = new VPINews.Model.News();
                mod_news.NewsID = id;
                bll_news.Delete(id);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('删除成功！');</script>");
                getNews();

            }
        }

    }



    //RowDataBound事件
    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FFFFCC';this.style.color='#000000';this.style.cursor='#FFFFCC';");
    //        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';this.style.color='#FFFFFF';");
    //    }
    //}

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        switch (((LinkButton)sender).CommandArgument.ToString())
        {
            //将不同CommandArgument参数  而同一CommandName名字的4个按钮 循环取CommandArgument参数 利用索引进行判断
            //从而实现分页功能 注意的一点是 4个 按钮必须要触发同一个  单击事件
            case "first":
                GridView1.PageIndex = 0;
                break;
            case "last":
                GridView1.PageIndex = GridView1.PageCount - 1;
                break;
            case "prev":
                GridView1.PageIndex = GridView1.PageIndex - 1;
                break;
            case "next":
                GridView1.PageIndex = GridView1.PageIndex + 1;
                break;

        }
        //之后 重新绑定
        getNews();
    }


    //删除GridView的一行
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteNews")
        {
            bll_news.Delete(Convert.ToInt32(e.CommandArgument));
            getNews();

        }
    }
    protected void listTypeName_SelectedIndexChanged(object sender, EventArgs e)
    {
        VPINews.BLL.News bll = new VPINews.BLL.News();
        DataSet ds = new DataSet();

        if (this.listTypeName.SelectedValue.ToString() == "显示所有新闻")
        {
            ds = bll.GetAllList();
        }

        else
        {
            ds = bll.GetList("TypeName='" + this.listTypeName.SelectedValue + "'");
        }
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}


